package com.foreknow.dao.impl;

import com.foreknow.dao.IBaseDao;
import com.foreknow.entity.User;
import com.foreknow.util.DbHelper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements IBaseDao<User> {
	private static QueryRunner queryRunner = DbHelper.getQueryRunner();

	@Override
	public int delete(String sql, Object... params) throws SQLException {
		return queryRunner.update(sql, params);
	}

	@Override
	public int delete(String sql) throws SQLException {
		return queryRunner.update(sql);

	}

	@Override
	public List<User> queryByCondition(String sql) throws SQLException {
		return queryRunner.query(sql, getListHandler());
	}

	@Override
	public int update(String sql, Object... params) throws SQLException {
		return queryRunner.update(sql, params);

	}

	@Override
	public int update(String sql, Object param) throws SQLException {
		return queryRunner.update(sql, param);
	}

	@SuppressWarnings("deprecation")
	@Override
	public User queryOne(String sql, Object param) throws SQLException {
		return queryRunner.query(sql, param, getResultSetHandler());

	}

	@SuppressWarnings("deprecation")
	public User queryOne(String sql, Object[] params) throws SQLException {
		return queryRunner.query(sql, params, getResultSetHandler());
	}

	@Override
	public ResultSetHandler<User> getResultSetHandler() {
		ResultSetHandler<User> handler = new ResultSetHandler<User>() {
			@Override
			public User handle(ResultSet resultSet) throws SQLException {
				User user = null;
				while (resultSet.next()) {
					Integer id = resultSet.getInt("id");
					String username = resultSet.getString("username");
					String account = resultSet.getString("account");
					String password = resultSet.getString("password");
					String phone = resultSet.getString("phone");
					String email = resultSet.getString("email");
					String avater = resultSet.getString("avater");
					user = new User(id, username, account, password, phone, email, avater);
				}
				return user;
			}
		};
		return handler;
	}

	public ResultSetHandler<List<User>> getListHandler() {
		ResultSetHandler<List<User>> handler = new ResultSetHandler<List<User>>() {
			@Override
			public List<User> handle(ResultSet resultSet) throws SQLException {
				List<User> users = new ArrayList<>();
				while (resultSet.next()) {
					Integer id = resultSet.getInt("id");
					String username = resultSet.getString("username");
					String account = resultSet.getString("account");
					String password = resultSet.getString("password");
					String phone = resultSet.getString("phone");
					String email = resultSet.getString("email");
					String avater = resultSet.getString("avater");
					User user = new User(id, username, account, password, phone, email, avater);
					users.add(user);
				}
				return users;
			}
		};
		return handler;
	}

	@Override
	public int insert(String sql, Object[] params) throws SQLException {
		return queryRunner.update(sql, params);

	}

	@Override
	public List<User> queryByCondition(String sql, Object... params) throws SQLException {
		return queryRunner.query(sql, getListHandler(),params);
	}
}
